﻿
using UnityEditor;
using UnityEngine;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2024 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shaco
{
    public class OnGUILayoutHelper
    {
        static public void SetEnumPopupOpen<T>(bool isOpen)
        {
            shaco.GameHelper.datasave.WriteBool(typeof(T).FullName, isOpen);
        }

        static public bool IsEnumPopupOpen<T>()
        {
            return shaco.GameHelper.datasave.ReadBool(typeof(T).FullName);
        }

        static public T EnumPopup<T>(T value, params GUILayoutOption[] options) where T : System.Enum
        {
            return EnumPopup(value, 1, options);
        }

        static public T EnumPopup<T>(T value, int colnum, params GUILayoutOption[] options) where T : System.Enum
        {
            // 模拟下拉菜单的样式
            GUILayout.BeginVertical("box", options);

            // 当点击时打开下拉菜单
            var isDropdownOpen = shaco.GameHelper.datasave.ReadBool(typeof(T).FullName);
            var arrow = isDropdownOpen ? "▼" : "▶";
            var title = $"{arrow}{value}";
            var style = new GUIStyle(GUI.skin.button);
            style.alignment = TextAnchor.MiddleLeft;
            if (GUILayout.Button(title, style, options))
            {
                isDropdownOpen = !isDropdownOpen;
                SetEnumPopupOpen<T>(isDropdownOpen);
            }
            GUI.changed = false;

            // 只有当下拉菜单打开时才显示选项
            if (isDropdownOpen)
            {
                var allEnum = System.Enum.GetValues(typeof(T));
                for (int i = 0; i < allEnum.Length; i += colnum)
                {
                    GUILayout.BeginHorizontal();
                    {
                        for (int j = 0; j < colnum; ++j)
                        {
                            var testIndex = i + j;
                            if (testIndex > allEnum.Length - 1)
                                break;

                            var option = (T)allEnum.GetValue(testIndex);
                            if (GUILayout.Button(option.ToString(), GUILayout.Height(70)))
                            {
                                if (value.Equals(option))
                                    GUI.changed = false;
                                else
                                {
                                    value = option;
                                    isDropdownOpen = false;
                                    SetEnumPopupOpen<T>(isDropdownOpen);
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }

            GUILayout.EndVertical();
            return value;
        }

        static public void SetPopupFieldOpen(string[] displayedOptions, bool isOpen)
        {
            if (null == displayedOptions || 0 == displayedOptions.Length)
                return;

            shaco.GameHelper.datasave.WriteBool(displayedOptions[0], isOpen);
        }

        static public bool IsPopupFieldOpen(string[] displayedOptions)
        {
            if (null == displayedOptions || 0 == displayedOptions.Length)
                return false;

            return shaco.GameHelper.datasave.ReadBool(displayedOptions[0]);
        }

        static public int PopupFiled(int selectedIndex, string[] displayedOptions, params GUILayoutOption[] options)
        {
            return PopupFiled(selectedIndex, displayedOptions, 1, options);
        }

        static public int PopupFiled(int selectedIndex, string[] displayedOptions, int colnum, params GUILayoutOption[] options)
        {
            if (null == displayedOptions || 0 == displayedOptions.Length)
            {
                GUILayout.Label("Empty PopupFiled");
                return -1;
            }

            if (selectedIndex < 0 || selectedIndex > displayedOptions.Length - 1)
            {
                selectedIndex = 0;
            }

            // 模拟下拉菜单的样式
            GUILayout.BeginVertical("box", options);
            var saveKey = displayedOptions[0];

            // 当点击时打开下拉菜单
            var isDropdownOpen = shaco.GameHelper.datasave.ReadBool(saveKey);
            var arrow = isDropdownOpen ? "▼" : "▶";
            var title = $"{arrow}{displayedOptions[selectedIndex]}";
            var style = new GUIStyle(GUI.skin.button);
            style.alignment = TextAnchor.MiddleLeft;
            if (GUILayout.Button(title, style, options))
            {
                isDropdownOpen = !isDropdownOpen;
                SetPopupFieldOpen(displayedOptions, isDropdownOpen);
            }
            GUI.changed = false;

            // 只有当下拉菜单打开时才显示选项
            if (isDropdownOpen)
            {
                for (int i = 0; i < displayedOptions.Length; i += colnum)
                {
                    GUILayout.BeginHorizontal();
                    {
                        for (int j = 0; j < colnum; ++j)
                        {
                            var testIndex = i + j;
                            if (testIndex > displayedOptions.Length - 1)
                                break;

                            var option = (string)displayedOptions.GetValue(testIndex);
                            if (GUILayout.Button(option.ToString(), GUILayout.Height(70)))
                            {
                                if (selectedIndex == testIndex)
                                    GUI.changed = false;

                                selectedIndex = testIndex;
                                isDropdownOpen = false;
                                SetPopupFieldOpen(displayedOptions, isDropdownOpen);
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }

            GUILayout.EndVertical();
            return selectedIndex;
        }

        static public int IntField(string title, int value, params GUILayoutOption[] options)
        {
            GUILayout.BeginHorizontal(options);
            {
                var style1 = new GUIStyle(GUI.skin.label);
                style1.alignment = TextAnchor.MiddleLeft;
                GUILayout.Label(title, style1, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true));
                GUI.changed = false;
                var style = new GUIStyle(GUI.skin.textField);
                style.alignment = TextAnchor.MiddleLeft;
                var outputStr = GUILayout.TextField(value.ToString(), style, GUILayout.ExpandHeight(true));
                if (GUI.changed)
                {
                    if (int.TryParse(outputStr, out var newIDParse))
                        value = newIDParse;
                    else
                    {
                        if (string.IsNullOrEmpty(outputStr))
                            value = 0;
                    }
                }
            }
            GUILayout.EndHorizontal();
            return value;
        }
    }
}
